package com.alt.www.leetCode;

/**
 * Created by wangyang on 2017/12/5.
 */
public class Solution {
    public static String longestPalindrome(String s) {
        char[] array = s.toCharArray();
        int maxLen=0,begin=0,end=0;
        for(int i=0;i<array.length;i++){
            char key = array[i];
            for(int j=array.length-1;j>0;j--){
                if(key==array[j]&&isCorrect(array,i, j)){
                    int len = j-i;
                    if(maxLen>=len){
                        break;
                    }
                    maxLen = len;
                    begin = i;
                    end = j;
                }
            }

        }
        StringBuilder builder = new StringBuilder();
        for(int index = begin;index<=end; index++){
            builder.append(array[index]);
        }
        System.out.println(builder.toString());
        return builder.toString();
    }

    private static boolean isCorrect(char[] array,int begin, int end){
        for(int i=begin, j=end;i<j;i++,j--){
            if(array[i]!=array[j])
                return false;
        }
        return true;
    }

    public static void main(String[] args) {
        longestPalindrome("babad");
        longestPalindrome("cbbd");
    }
}
